#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/11/24 15:57
@File: radio.py
"""
from qturboframe.lib.utils.by import By
from qturboframe.public_ui.h_ui.radio import Radio as HuiRadio
from qturboframe.lib.webdriver.remote.webelement import WebElement
from qturboframe.lib.webdriver.remote.webdriver import WebDriver

from BladeFerm.Lib.Common import logger
from BladeFerm.hui12_common.function import get_params


class Radio(HuiRadio):

    def __init__(self, element_para, father=None, timeout=5):
        if isinstance(element_para, list) and len(element_para) > 0:
            if isinstance(element_para[0], WebElement):
                self._elements = element_para
                self._driver = element_para[0]._parent
                self.timeout = timeout
            else:
                raise AttributeError('element_para参数不正确，需是WebElement的集合！')
        elif not isinstance(element_para, dict):
            raise AttributeError('element_para参数类型不正确！')
        else:
            if father is None:
                raise AttributeError('未定位元素，father参数不能为None！')
            if isinstance(father, WebDriver):
                self._driver = father
                self.timeout = timeout
            elif isinstance(father, WebElement):
                self._driver = father._parent
                self.timeout = timeout
            else:
                raise AttributeError('father参数类型不正确！')
            element_by, element_value, assist = get_params(element_para)
            self._element = father.find_element_by_attribute(
                element_by, element_value, assist, self.timeout)
            self._elements = self._element.find_elements_by_attribute('xpath',
                                                                      './label[contains(@class, "h-radio-wrapper")]')

    def set_value(self, text):
        """
        hui的单选按钮控件选择

        :Args:
         - text: 选择项的文本

        :Example:
         - 例: radio.set_value('苹果')
        """
        for ele in self._elements:
            logger.warning(ele.text)
            radio_text = ele.text
            logger.debug(f"radio_text: {radio_text}")
            if text == radio_text:
                ele.secure_click()
                logger.info(f"set value : {radio_text}")
                break